package com.rongshu.view.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.SpannableStringUtils;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.home.entity.RecomendMsg;
import com.rongshu.view.home.widget.SelectableTextView;
import com.rongshu.view.userInfo.activity.PersonDetailsActivity;
import com.widget.CircleImageView;
import com.widget.toast.ToastUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class ArticleDetailsRecomendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //context
    private Context mContext = null;
    //list
    private List<RecomendMsg.ResponseBean> responseBeanList = null;
    //interface
    private OnItemClickListener mOnItemClickListener = null;
    //intent
    private Intent intent = null;

    public ArticleDetailsRecomendAdapter(Context context) {
        this.mContext = context;
    }

    public void setResponseBeanList(List<RecomendMsg.ResponseBean> list) {
        this.responseBeanList = list;
    }

    public void addResponseBeanList(List<RecomendMsg.ResponseBean> list) {
        this.responseBeanList.addAll(list);
    }

    public List<RecomendMsg.ResponseBean> getResponseBeanList() {
        return this.responseBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_article_details_item_view, parent, false);
        return new ArticleDetailsRecomendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RecomendMsg.ResponseBean responseBean = responseBeanList.get(position);
        ArticleDetailsRecomendViewHolder viewHolder = (ArticleDetailsRecomendViewHolder) holder;
        try {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.user_head_icon);
            Glide.with(mContext)
                    .load(responseBean.getAvatar())
                    .apply(options)
                    .into(viewHolder.iv_head_icon);
            viewHolder.iv_vip.setVisibility(responseBean.isVip() ? View.VISIBLE : View.GONE);
            if (responseBean.getTotalCount() > 1) {
                String name = responseBean.getFrom_Nmae();
                String dealContent = name + " (" + responseBean.getTotalCount() + ")";
                viewHolder.txt_user_name.setText(SpannableStringUtils.addForeColorSpan(dealContent,
                        mContext.getResources().getColor(R.color.color_3cc051), name.length(), null));
            } else {
                viewHolder.txt_user_name.setText(responseBean.getFrom_Nmae());
            }
            viewHolder.txt_time.setText(DateUtils.ConvertDate(responseBean.getCreate_datetime()));
            String content = responseBean.getContents().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            viewHolder.txt_content.setText(URLDecoder.decode(content, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(position);
                }
            }
        });

        viewHolder.iv_head_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //user_id
                int user_id = getUserId();
                if (user_id == 0) {
                    LoginUtils.toLogin(mContext);
                    return;
                }
                if (user_id == responseBean.getFrom_uid()) {
                    intent = new Intent(mContext, PersonDetailsActivity.class);
                    mContext.startActivity(intent);
                } else {
                    int is_allow_browse_homepage = responseBean.getIs_allow_browse_homepage();
                    if (is_allow_browse_homepage == 0) {
                        ToastUtils.show("由于对方的设置，你暂时无法浏览他的主页");
                        return;
                    }
                    intent = new Intent(mContext, UserHomePageActivity.class);
                    intent.putExtra(Constant.USER_ID, responseBean.getFrom_uid());
                    mContext.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return responseBeanList == null ? 0 : responseBeanList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public static class ArticleDetailsRecomendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head_icon)
        CircleImageView iv_head_icon;
        @BindView(R.id.iv_vip)
        ImageView iv_vip;
        @BindView(R.id.txt_user_name)
        TextView txt_user_name;
        @BindView(R.id.txt_time)
        TextView txt_time;
        @BindView(R.id.txt_content)
        SelectableTextView txt_content;

        public ArticleDetailsRecomendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //interface
    public interface OnItemClickListener {
        void onClick(int position);
    }
}
